home *** CD-ROM | disk | FTP | other *** search
- /*
- * output.c
- *
- * Practical Algorithms for Image Analysis
- *
- * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
- */
-
- /*
- * OUTPUT.C
- *
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdarg.h>
- #include "vor.h"
-
-
- #define XL 0.0
- #define XR (float)(WIDTH - 1)
- #define YU 0.0
- #define YL (float)(HEIGHT - 1)
-
-
- extern int TRIANGULATE, DBG;
- extern int WRITE_FLAG;
-
- extern FILE *fpOut;
-
-
- float pxmin, pxmax, pymin, pymax, cradius;
- int DISPL_PAGE = 1;
- int RESET = 1; /* when set, reset screen */
-
- /*
- * plotinit()
- * DESCRIPTION:
- * initialize various parameters
- * ARGUMENTS:
- * imgIO: image for drawing (Image *)
- * RETURN VALUE:
- * none
- */
-
- void
- plotinit (Image * imgIO)
- {
- float dx, dy, d;
- float xl, yl, xr, yu;
-
- dy = ymax - ymin;
- dx = xmax - xmin;
- d = (dx > dy ? dx : dy) * (float) 1.3;
- xl = (float) 0.0;
- yu = (float) 0.0;
- xr = (float) imgIO->width - 1;
- yl = (float) imgIO->height - 1;
-
- if ((pxmin = xmin - (d - dx) / (float) 2.0) < xl)
- pxmin = (float) xl;
- if ((pxmax = xmax + (d - dx) / (float) 2.0) > xr)
- pxmax = (float) xr;
- if ((pymin = ymin - (d - dy) / (float) 2.0) < yu)
- pymin = (float) yu;
- if ((pymax = ymax + (d - dy) / (float) 2.0) > yl)
- pymax = (float) yl;
-
- cradius = (pxmax - pxmin) / (float) 350.0;
-
- }
-
-
-
-
- /*
- * gprintf()
- * DESCRIPTION:
- * write to std output and, if option WRITE_FILE set, to file wbuf (stream)
- * ARGUMENTS:
- * fpOut: pointer to open FILE
- * va_list...
- * RETURN VALUE:
- * none
- */
-
- void
- gprintf (FILE * fpOut, char *fmt,...)
- {
- va_list arg_ptr;
-
- va_start (arg_ptr, fmt);
- vprintf (fmt, arg_ptr);
- if (WRITE_FLAG == 1)
- vfprintf (fpOut, fmt, arg_ptr);
- va_end (arg_ptr);
- }
-
- /*
- * by construction, e->a or e->b assume the value 1.0
- * (see routine bisect() in geometry.c)
- */
- int
- clip_line (e, imgIO, value)
- struct Edge *e;
- Image *imgIO;
- int value;
- {
- struct Site *s1, *s2;
- float x1, x2, y1, y2;
-
- if (e->a == 1.0 && e->b >= 0.0) { /* vertical line: x = c/a */
- s1 = e->ep[1];
- s2 = e->ep[0];
- }
- else {
- s1 = e->ep[0];
- s2 = e->ep[1];
- }
-
- if (e->a == 1.0) { /* x = c - b*y */
- y1 = pymin;
- if ((s1 != (struct Site *) NULL) && (s1->coord.y > pymin))
- y1 = s1->coord.y;
- if (y1 > pymax)
- return (0);
- x1 = e->c - e->b * y1;
-
- y2 = pymax;
- if ((s2 != (struct Site *) NULL) && (s2->coord.y < pymax))
- y2 = s2->coord.y;
- if (y2 < pymin)
- return (0);
- x2 = e->c - e->b * y2;
-
- if ((x1 > pxmax & x2 > pxmax) | (x1 < pxmin & x2 < pxmin))
- return (0);
-
-
- if (x1 > pxmax) {
- x1 = pxmax;
- y1 = (e->c - x1) / e->b;
- }
- if (x1 < pxmin) {
- x1 = pxmin;
- y1 = (e->c - x1) / e->b;
- }
- if (x2 > pxmax) {
- x2 = pxmax;
- y2 = (e->c - x2) / e->b;
- }
- if (x2 < pxmin) {
- x2 = pxmin;
- y2 = (e->c - x2) / e->b;
- }
- }
- else {
- x1 = pxmin;
- if ((s1 != (struct Site *) NULL) && (s1->coord.x > pxmin))
- x1 = s1->coord.x;
- if (x1 > pxmax)
- return (0);
- y1 = e->c - e->a * x1;
-
- x2 = pxmax;
- if ((s2 != (struct Site *) NULL) && (s2->coord.x < pxmax))
- x2 = s2->coord.x;
- if (x2 < pxmin)
- return (0);
- y2 = e->c - e->a * x2;
-
- if ((y1 > pymax & y2 > pymax) | (y1 < pymin & y2 < pymin))
- return (0);
-
-
- if (y1 > pymax) {
- y1 = pymax;
- x1 = (e->c - y1) / e->a;
- }
- if (y1 < pymin) {
- y1 = pymin;
- x1 = (e->c - y1) / e->a;
- }
- if (y2 > pymax) {
- y2 = pymax;
- x2 = (e->c - y2) / e->a;
- }
- if (y2 < pymin) {
- y2 = pymin;
- x2 = (e->c - y2) / e->a;
- }
- }
- draw_line ((int) x1, (int) y1, (int) x2, (int) y2, imgIO, value);
- }
-
- void
- out_bisector (e, imgIO, value)
- struct Edge *e;
- Image *imgIO;
- int value;
- {
- if (TRIANGULATE & !DBG)
- draw_line ((int) e->reg[0]->coord.x, (int) e->reg[0]->coord.y, (int) e->reg[1]->coord.x, (int) e->reg[1]->coord.y, imgIO, value);
-
- if (!TRIANGULATE & !DBG)
- gprintf (fpOut, "l %f %f %f\n", e->a, e->b, e->c);
-
- if (DBG)
- gprintf (fpOut, "line(%d) %gx+%gy=%g, bisecting %d %d\n",
- e->edgenbr, e->a, e->b, e->c, e->reg[le]->sitenbr, e->reg[re]->sitenbr);
- }
-
-
- void
- out_ep (e, imgIO, value)
- struct Edge *e;
- Image *imgIO;
- int value;
- {
- if (!TRIANGULATE)
- clip_line (e, imgIO, value);
-
- if (!TRIANGULATE) {
- gprintf (fpOut, "e %d", e->edgenbr);
- gprintf (fpOut, " %d ", e->ep[le] != (struct Site *) NULL ? e->ep[le]->sitenbr : -1);
- gprintf (fpOut, "%d ", e->ep[re] != (struct Site *) NULL ? e->ep[re]->sitenbr : -1);
- gprintf (fpOut, "%d %d\n", e->reg[le]->sitenbr, e->reg[re]->sitenbr);
- }
- }
-
- void
- out_vertex (v)
- struct Site *v;
- {
- if (!TRIANGULATE & !DBG)
- gprintf (fpOut, "v %f %f\n", v->coord.x, v->coord.y);
- if (DBG)
- gprintf (fpOut, "vertex(%d) at %f %f\n", v->sitenbr, v->coord.x, v->coord.y);
- }
-
-
- void
- out_site (s, imgIO, value)
- struct Site *s;
- Image *imgIO;
- int value;
- {
- if (!TRIANGULATE & !DBG)
- draw_circle ((int) s->coord.x, (int) s->coord.y, (int) cradius, imgIO, value);
- if (!TRIANGULATE & !DBG)
- gprintf (fpOut, "s %f %f\n", s->coord.x, s->coord.y);
- if (DBG)
- gprintf (fpOut, "site(%d) at %f %f\n", s->sitenbr, s->coord.x, s->coord.y);
- }
-
-
- void
- out_triple (s1, s2, s3)
- struct Site *s1, *s2, *s3;
- {
- if (TRIANGULATE & !DBG)
- gprintf (fpOut, "%d %d %d\n", s1->sitenbr, s2->sitenbr, s3->sitenbr);
- if (DBG)
- printf ("circle through left=%d right=%d bottom=%d\n",
- s1->sitenbr, s2->sitenbr, s3->sitenbr);
- }
-